import { asyncRouterMap, constantRouterMap } from '@/router/index';
import { menuList } from '@/api/site.js'
import store from '@/store'

const getComponent = function(pathList, routerList){
  let componentLsit = []
  componentLsit = getComponentList(pathList, routerList, componentLsit)
  return  componentLsit
}
const getComponentList  = function(pathList, routerList, componentLsit){
  for (let i = 0; i < routerList.length; i++) {
    const ele = routerList[i];
    for (let j = 0; j < pathList.length; j++) {
      const path = pathList[j];
      if (path == ele.path){
        componentLsit[path] = ele.component
      }
      
    }
    if (ele.children && ele.children.length > 0){
      getComponentList(pathList, ele.children, componentLsit)
    }
    
  }
  return  componentLsit
}

const getPath = function( menuList){
  let pathList = []
  let showMenuList = []
  getPathItem(menuList, pathList, showMenuList)
  return {pathList: pathList, showMenuList:showMenuList}
}

const getPathItem = function(menuList ,pathList, showMenuList){
  for (let i = 0; i < menuList.length; i++) {
    const ele = menuList[i];
    if (ele.status != 'hidden'){
      showMenuList[ele.path] = ele
      pathList.push(ele.path)
    }
    if(ele.childlist && ele.childlist.length > 0){
      getPathItem(ele.childlist ,pathList,showMenuList)
    }
  }
} 

const hasPermission = function(data,pathList, componentList){
  if(!data){
    return;
  }
  data.forEach(ele => {
    ele.meta = {
      title: ele.name,
      icon: ele.icon,
      hidden: true
    }
    let childlist = ele.childlist 
    
    if (childlist && ele.childlist.length > 0) {
      ele.children = ele.childlist
    }
    if(pathList.indexOf(ele.path) == -1 || ele.status == 'hidden' ){
      ele.meta.hidden = true
    } else {
      ele.meta.hidden = false
      ele.component = componentList[ele.path]
    }
    ele.children && hasPermission(ele.children, pathList, componentList)
  });
  return data
}

const permission = {
  state:{
    routers: constantRouterMap,
    addRouters: [],
    menuList: [],
    path_show: []
  },
  mutations:{
    SET_ROUTERS: (state, routers) => {
      state.routers = routers;
    },
    SET_MENUlIST: (state, data) => {
      state.menuList = data;
    },
    SET_PATH_SHOW: (state, data) => {
      state.path_show = data;
    },
  },
  actions:{
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        let params = {
          site_id: store.state.site.curSite.id
        }
        menuList(params).then(res=>{
          if (res.state ==1){
            let menuList = res.data.rows
            commit('SET_MENUlIST', menuList);
            let path = getPath(menuList)
            let pathList = path.pathList
            let componentsList = getComponent(pathList, asyncRouterMap)
            commit('SET_PATH_SHOW', pathList)
            const accessedRouters = hasPermission(menuList,pathList, componentsList)
            commit('SET_ROUTERS', accessedRouters);
          }
        })
        resolve();
      })
    }
  }
}
export default permission